package com.jingdianjichi.sunject.application.controller;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBo;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.sunject.application.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.sunject.application.dto.SubjectLabelDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 标签模块
 */
@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {

    @Resource
    SubjectLabelDomainService subjectLabelDomainService;

    /**
     * 新增标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/add")
    public Result addLabel(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if(log.isInfoEnabled()){
            log.info("addLabel-----SubjectLabelController:{}", JSON.toJSONString(subjectLabelDTO));
        }
        Preconditions.checkNotNull(subjectLabelDTO.getLabelName(),"标签名字不能为空");
        Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(),"标题种类不能为空");
        Preconditions.checkNotNull(subjectLabelDTO.getSortNum(),"排序不能为空");

        try {
            SubjectLabelBo subjectLabelBo = SubjectLabelDTOConverter.INSTANCE
                    .convertDtoToBo(subjectLabelDTO);
            subjectLabelDomainService.add(subjectLabelBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.info("错误日志---add-----SubjectLabelController:{}",e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 更新标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/update")
    public Result updateLabel(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if(log.isInfoEnabled()){
            log.info("updateLabel-----SubjectLabelController:{}", JSON.toJSONString(subjectLabelDTO));
        }
        Preconditions.checkNotNull(subjectLabelDTO.getId(),"更新标签Id不能为空");

        try {
            SubjectLabelBo subjectLabelBo = SubjectLabelDTOConverter.INSTANCE
                    .convertDtoToBo(subjectLabelDTO);
            subjectLabelDomainService.updateLabel(subjectLabelBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.info("错误日志---updateLabel-----SubjectLabelController:{}",e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 删除标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/delete")
    public Result deleteLabel(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if(log.isInfoEnabled()){
            log.info("deleteLabel-----SubjectLabelController:{}", JSON.toJSONString(subjectLabelDTO));
        }
        Preconditions.checkNotNull(subjectLabelDTO.getId(),"标签Id不能为空");

        try {
            SubjectLabelBo subjectLabelBo = SubjectLabelDTOConverter.INSTANCE
                    .convertDtoToBo(subjectLabelDTO);
            subjectLabelDomainService.deleteLabel(subjectLabelBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.info("错误日志---deleteLabel-----SubjectLabelController:{}",e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if(log.isInfoEnabled()){
            log.info("queryLabelByCategoryId-----SubjectLabelController:{}", JSON.toJSONString(subjectLabelDTO));
        }
        Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(),"分类Id不能为空");

        try {
            SubjectLabelBo subjectLabelBo = SubjectLabelDTOConverter.INSTANCE
                    .convertDtoToBo(subjectLabelDTO);
            List<SubjectLabelBo> subjectLabelBoList=subjectLabelDomainService
                    .queryLabelByCategoryId(subjectLabelBo);
            List<SubjectLabelDTO> subjectLabelDTOList = SubjectLabelDTOConverter.INSTANCE
                    .convertBoListToDtoList(subjectLabelBoList);
            return Result.ok(subjectLabelDTOList);
        } catch (Exception e) {
            log.info("错误日志---deleteLabel-----SubjectLabelController:{}",e.getMessage());
            return Result.fail(e.getMessage());
        }
    }
}
